const bailRE = /[^\w.$]/
export function parsPath (path) {
    if(bailRE.text(path)){
        return 
    }
    const segments = path.split('.')
    return function (obj) {
        for(let i=0; i<segments.length; ++i){
            if(!obj) return
            // 不停变更obj储存的地址值，类似链表里的p指针
            obj = obj[segments[i]]
        }
        return obj
    }
}
export default class Watcher {
    constructor(vm, expOrFn, cb) {
        this.vm = vm
        this.getter = parsPath(expOrFn) 
        this.cb = cb
        this.value = this.get()
    }
    get() {
        window.target = this
        let value =  this.getter.call(this.vm, this.vm)
        window.target = null
        return value
    }
    update() {
        const oldValue = this.value
        // 取新值   会触发 getter
        this.value = this.get()
        this.cb.call(this.vm, this.value, oldValue)
    }
}